package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

@Service

public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void  saveWithFlavor (DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.insert(dish) ;

        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor f : flavors) {
                f.setDishId(dish.getId());
            }
            dishFlavorMapper.insertBatch(flavors) ;
        }


    }


    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO) ;

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id批量删除菜品
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        //判断菜品是否在销售
        for (Long id : ids) {
            Dish  dish = dishMapper.getById (id) ;
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE) ;
            }
        }
        //判断要删除的菜品是否在套餐之中

        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids) ;

        if (setmealIds.size() > 0 && setmealIds != null) {
            throw  new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE) ;
        }


//        for (Long id : ids) {
//            //根据id删除菜品
//            dishMapper.deleteByid (id) ;
//            //根据id删除口味
//            dishFlavorMapper.deleteByDishId (id) ;
//        }
        //根据id批量删除菜品
        dishMapper.deleteByids (ids) ;

        //根据dish_id删除口味
        dishFlavorMapper.deleteByDishIds (ids);
    }


    /**
     * 根据id回显菜品信息
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdWithFlavor(Long id) {

        //根据id查询菜品信息
        Dish dish = dishMapper.getById(id);
        //根据dishId查询菜品信息
        List<DishFlavor> flavors =  dishFlavorMapper.getByDishId (id) ;

        DishVO dishVO = new DishVO();

        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);

        return dishVO;
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     */
    @Override
    public void updateDishWithFlavor(DishDTO dishDTO) {
        //修改菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update (dish) ;

        //删除口味数据

        dishFlavorMapper.deleteByDishId(dishDTO.getId());


        //插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor f : flavors) {
                f.setDishId(dish.getId());
            }
            dishFlavorMapper.insertBatch(flavors) ;
        }
    }

    /**
     * 根据categoryId查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> listByCategoryId(Long categoryId) {

        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE).build();

        List<Dish> dishes = dishMapper.list(dish) ;

        return dishes ;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
